<template>
    <div class="addadmin" style="width: 1280px;">
        <div class="admin_header">
            <div class="administrator_header">
                <div class="ax_default box_2" id="u9509">
                    <div id="u9509_div"></div>
                </div>
                <span class="header_words" v-show="type">新增职称</span>
                <span class="header_words" v-show="!type">更新职称</span>
            </div>

        </div>
        <div class="add_form">
            <div class="form_header">
                <span class="form_admin" v-show="type">编辑角色信息</span>
                <span class="form_admin" v-show="!type">更新角色信息</span>
            </div>
            <div class="form_body">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="角色名称" prop="role">
                        <el-input v-model="ruleForm.role" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="角色描述" prop="roleDescribe">
                        <el-input v-model="ruleForm.roleDescribe" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="是否启用" prop="setMealState">
                        <el-switch v-model="ruleForm.setMealState"></el-switch>
                    </el-form-item>
                    <div class="form_check">
                        <div class="check_word"><span>权限设置</span></div>
                        <div class="check_list" style="width: 956px;">
                            <div class="check_doctor">
                                <el-checkbox class="check_box" v-model="allChecked" @change="handleAllChecked">
                                    <span class="list_text">医生管理</span>
                                </el-checkbox>
                            </div>
                            <div class="list_item">
                                <div v-for="(item, index) in list" :key="index" class="item_checkbox">
                                    <!-- <input type="checkbox" class="check_box" v-model="item.isChecked" @change="handleChecked(item, $event)">{{ item.text }} -->
                                    <el-checkbox class="check_box" v-model="item.isChecked"
                                        @change="handleChecked(item, $event)">
                                        <span class="list_text">{{ item.text }}</span>

                                    </el-checkbox>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form_point"><span>....</span></div>

                    <el-form-item class="form_btn">
                        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>

                        <el-button type="primary" plain @click="resetForm()">取消</el-button>
                    </el-form-item>
                </el-form>



            </div>


        </div>

    </div>
</template>
<script>
import { addUser, updataUserData } from '@/api/systemRole'
import createDate from '@/utils/createDate'
import { nanoid } from 'nanoid'
export default {
    name: 'Addrole',
    data() {
        return {

            ruleForm: {
                role: '',
                numberPeople: 3,
                // description: '',
                roleDescribe: '',
                setMealState: false,
                addDate: '',
                isdelete: 0,

            },
            type: true,
            allChecked: false,

            list: [
                { id: 1, text: '新增医生信息', },
                { id: 2, text: '删除医生信息', },
                { id: 3, text: '导入', },
                { id: 4, text: '导出', },
                { id: 5, text: '编辑医生信息', },
                { id: 6, text: '批量删除医生信息', },
            ],

            rules: {
                role: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                ],
            },
            type: true
        }

    },
    watch: {
        list: { // 监听事件,监听复选框是否全部选中,全部选中则全选的复选框勾选上
            handler(val) {
                var i = 0
                this.list.forEach(item => {
                    if (item.isChecked === true) {
                        i++
                    }
                    if (i === this.list.length) {
                        this.allChecked = true
                    } else {
                        this.allChecked = false
                    }
                })
            },
            deep: true
        }
    },

    created() {
        this.list.forEach(item => {
            // 添加判断的字段
            this.$set(item, 'isChecked', false)
        })
    },

    methods: {

        handleAllChecked(v) { // 实现全选，反选(点击会传递true或者false过来)
            this.list.map(item => {
                item.isChecked = v
            })
        },
        handleChecked(item, e) { // 单个选中
            item.isChecked = e
        },
        // 判断是添加用户 还是更新用户 方法  
        isAdd() {

            if (this.$route.params.type == 'updata') {
                this.type = false
                let _obj = this.$route.params._userObj

                this.ruleForm.role = _obj.role
                this.ruleForm.roleDescribe = _obj.roleDescribe
                this.ruleForm.setMealState = _obj.setMealState
                this.ruleForm.isChecked = _obj.isChecked
            } else {
                this.type = true
            }
        },



        async submitForm() {
            // 判断是添加用户 还是更新用户
            if (this.type) {
                // 自定义方法 生成时间 yyyy-MM-dd  hh:mm:ss
                if (
                    this.ruleForm.role.trim() != '' &&
                    this.ruleForm.roleDescribe.trim() != ''
                ) {
                    let time = new Date()
                    let nowTime = createDate(time.toLocaleString('en-US', { hour12: false }).split(" "))

                    let _obj = {
                        id: nanoid(),
                        role: this.ruleForm.role,
                        roleDescribe: this.ruleForm.roleDescribe,
                        setMealState: this.ruleForm.setMealState,
                        addDate: nowTime,
                        isChecked: false,
                        isdelete: 0
                    }
                    // 添加用户
                    await addUser(_obj)
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.$router.push('/rolement')
                } else {
                    this.$message({
                        message: '请不要输入空内容',
                        type: 'error'
                    })
                }
            } else {
                if (this.ruleForm.role.trim() != '' &&
                    this.ruleForm.roleDescribe.trim() != '') {
                    let time = new Date()
                    let nowTime = createDate(time.toLocaleString('en-US', { hour12: false }).split(" "))
                    this.ruleForm.addDate = nowTime

                    await updataUserData(this.$route.params._userObj.id, this.ruleForm)

                    this.$message({
                        message: '更新成功',
                        type: 'success'
                    })
                    this.$router.push('/Rolement')
                } else {
                    this.$message({
                        message: '请不要输入空内容',
                        type: 'error'
                    })
                }

            }
        },
        //取消按钮
        resetForm() {
            this.$router.go(-1);
        }


    },
    mounted() {
        //判断是添加用户，还是更新用户
        this.isAdd()
        // console.log(this.type);
    }
}
</script>
<style lang="scss" scoped>
.addadmin {
    margin: 0 auto;
}

.list_text {
    color: rgb(24, 24, 24);
}

.admin_header {
    display: flex;
    justify-content: space-between;
}

.administrator_header {
    display: flex;
    align-items: center;
}

#u9509 {
    border-width: 0px;
    width: 24px;
    height: 8px;
    display: flex;
    font-size: 16px;
    color: #006EFF;

}

#u9509_div {
    border-width: 0px;
    width: 24px;
    height: 8px;
    background: inherit;
    background-color: rgba(0, 110, 255, 1);
    border: none;
    border-radius: 4px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    font-size: 16px;
    color: #006EFF;
}

.header_words {
    margin-left: 20px;
    font-size: 24px;
}

.add_form {
    box-shadow: 0 0 10px #ccc;
    border-radius: 10px;
    margin-top: 20px;
    background-color: #fff;
}

.demo-ruleForm {
    width: 420px;
}

.form_header {
    border-radius: 10px 10px 0 0;
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    background-color: rgba(242, 242, 242, 1);
    padding-left: 20px;
}

.form_admin {
    display: inline-block;
    color: #006EFF;
}

.form_body {
    margin-top: 10px;
    padding: 10px 0;
}

.form_check {
    padding: 0 20px 20px 20px;
}

.check_word {
    font-size: 18px;
}

.check_list {
    margin-top: 20px;
    border: 1px solid #ccc;
    border-radius: 2px;
    font-size: 15px;
}

.check_doctor {
    padding-left: 20px;
    height: 50px;
    line-height: 50px;
    background-color: rgba(0, 110, 255, 0.0470588235);
}

.check_box {
    width: 15px;
    height: 15px;
    margin-right: 6px;
}

.list_item {
    width: 956px;
    display: flex;
    padding: 20px;
    flex-wrap: wrap;
}

.item_checkbox {
    width: 20%;
    word-break: break-all;

}

.item_checkbox:nth-child(6) {
    margin-top: 20px;
}

.form_point {
    padding: 0 40px;
}

.el-button {
    padding: 12px 45px;
    font-size: 14px;
}

.form_btn {
    margin-top: 50px;
}
</style>